<?php
/**
 * Created by JetBrains PhpStorm.
 * User: huan
 * Date: 14-3-18
 * Time: 下午11:58
 * To change this template use File | Settings | File Templates.
 */

/**
 * 验证码
 * Class VerifyCode
 */
class VerifyCode{

    const LoginVerifyCode = 'LoginVerifyCode';
    /**
     * 生成验证码
     * @return string
     */
    function create_verify_code($len = 6){

        $code = '';
        for($i = 0;$i < $len;$i++){
            $code .= (string)rand(0,9);
        }
        return $code;
    }

    /**
     * 生成session验证码
     */
    function create_session_verify(){
        $code = $this->create_verify_code();

        session(LoginVerifyCode,$code);

        return $code;
    }

    /**
     * 验证是否正确
     * @param $code
     * @return bool
     */
    function check_session_verify($code){
        if(session('?'.LoginVerifyCode)){
            if(session(LoginVerifyCode) == $code){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}